package com.quxuecx.springcloud.controller;

import com.quxuecx.springcloud.entities.CommentResult;
import com.quxuecx.springcloud.entities.Payment;
import com.quxuecx.springcloud.service.IPaymentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

/**
 * @author: shaofeer
 * <p>
 * @qq: 337081267
 * <p>
 * @CSDN: http://blog.csdn.net/pyfysf
 * <p>
 * @blog: http://wintp.top
 * <p>
 * @email: shaofeer@163.com
 * <p>
 * @time: 2020/6/16
 */
@Slf4j
@RestController
@RequestMapping("/payment")
public class PaymentController {
    @Value("${server.port}")
    private String port;

    @Autowired
    IPaymentService mIPaymentService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/port")
    public String getPort() {
        return port;
    }

    @GetMapping(value = "/discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            log.info("***** element:" + element);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-PAYMENT");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }
        return this.discoveryClient;
    }


    /**
     * 保存数据
     *
     * @param payment
     * @return
     */
    @PostMapping("/save")
    public CommentResult save(@RequestBody Payment payment) {
        int row = mIPaymentService.save(payment);
        log.info("插入结果：" + row);
        if (row > 0) {
            CommentResult success = new CommentResult().success(null);
            success.setMessage("port::" + port);
            return success;
        } else {
            CommentResult error = new CommentResult().error();
            error.setMessage("port::" + port);

            return error;
        }
    }

    /**
     * 查询数据
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public CommentResult<Payment> getById(@PathVariable("id") Integer id) {
        Payment payment = mIPaymentService.getById(id);

        log.info("查询结果：{}", payment);

        CommentResult<Payment> success = new CommentResult<Payment>().success(payment);
        success.setMessage("port:::" + port);
        return success;

    }
}
